package com.ch.home.service.serviceImpl.sourceServiceImpl;

import com.ch.home.bean.Source;
import com.ch.home.mapper.sourceMapper.SourceMapper;
import com.ch.home.service.serviceInterface.sourceService.SourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;

/**
 * Created by CHEN on 2019/8/26.
 */
@Service
public class SourceServiceImpl implements SourceService {

    @Autowired
    SourceMapper sourceMapper;

    @Override
    public HashMap addSource(Source source) {
        HashMap<Object, Object> result = new HashMap<>( );

        int re = sourceMapper.addSource(source);

        if (re == 1) {
            result.put("message", "seccess");
        } else {
            result.put("message", "fail");
        }
        return result;
    }

    @Override
    public HashMap updateSource(Source source) {

        HashMap<Object, Object> result = new HashMap<>( );

        int re = sourceMapper.updateSource(source);

        if (re == 1) {
            result.put("message", "seccess");
        } else {
            result.put("message", "fail");
        }
        return result;
    }

    @Override
    public HashMap getPreviewSourceByCondition(Source source) {

        HashMap<Object, Object> result = new HashMap<>( );

        LinkedList<Source> sources = sourceMapper.getSourceByCondition(source);

        if (sources.size()>0){
            for (Source s:sources
                 ) {
                s.setUrl(null);
            }
            result.put("message", "seccess");

        }else {
            result.put("message", "fail");
        }
        result.put("value",sources);
        return result;
    }

    @Override
    public HashMap getSourceByCondition(Source source) {
        HashMap<Object, Object> result = new HashMap<>( );

        LinkedList<Source> sources = sourceMapper.getSourceByCondition(source);

        if (sources.size()>0){
            for (Source s:sources
                    ) {
                s.setSourcePreviewUrl(null);
            }
            result.put("message", "seccess");

        }else {
            result.put("message", "fail");
        }
        result.put("value",sources);
        return result;
    }

    @Override
    public HashMap getAllSourceByCondition(Source source) {
        HashMap<Object, Object> result = new HashMap<>( );

        LinkedList<Source> sources = sourceMapper.getSourceByCondition(source);

        if (sources.size()>0){
            result.put("message", "seccess");
        }else {
            result.put("message", "fail");
        }
        result.put("value",sources);
        return result;
    }
}
